package com.hangsu.train.components.convert;

import com.hangsu.train.components.entity.Permiss;
import com.hangsu.train.components.entity.Role;
import com.hangsu.train.components.vo.MenuVo;
import com.hangsu.train.components.vo.UserRoleVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Mapper(componentModel = "spring")
public interface SecurityConvert {

    /**
     * Permiss to MenuRespDto
     *
     * @param permiss
     * @return
     */
    @Mappings({
            @Mapping(source = "name", target = "label"),
            @Mapping(source = "id", target = "key")
    })
    MenuVo convert(Permiss permiss);

    default List<UserRoleVo> convert(List<Role> roleList, List<String> roleCodes) {
        if (roleList == null || roleList.isEmpty()) {
            return new ArrayList<>();
        }
        return roleList.stream().map(role -> {
            UserRoleVo dto = new UserRoleVo();
            dto.setCode(role.getCode());
            dto.setName(role.getName());
            dto.setHasRole(roleCodes.contains(role.getCode()));
            return dto;
        }).collect(Collectors.toList());
    }
}
